package com.youyou.action;

import com.youyou.entity.Account;
import com.youyou.mapper.AccountMapper;
import com.youyou.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

@Controller
@SessionAttributes("account")
public class LoginAction {
    @Autowired
    private AccountService accountService;
    //这是登陆页面
    @RequestMapping("/loginView")
    public String loginView(){
        return "login";
    }

    //登录时检查的业务逻辑
    @RequestMapping("/loginCheck")
    public String loginCheck(String userName, String password, Model model){
        System.out.println(userName+"  "+password);
        //判断用户是否登陆成功
        Account account = accountService.login(userName, password);
        if (account==null){

            model.addAttribute("message","LOGIN_ERROR");
            return "redirect:loginView";

        }
        model.addAttribute("account",account);
        //存放session
        return "redirect:index";
    }

    @RequestMapping("/btnexit")
    public  String exit(){
        return "login";
    }
}
